Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support iterators on GFK fields when using _quantity param #207

Merged
merged 4 commits into from
Jun 18, 2021

Conversation

abbottc
Copy link
Contributor

@abbottc abbottc commented Jun 17, 2021

Fixes #166

Copy link
Member

@berinhard berinhard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix @abbottc! The only thing I'll have to ask is for you to also update the changelog file. Just copying and pasting the PR title and PR id to it is enough.

I'll wait for an extra review from @amureki @anapaulagomes or @timjklein36 to get this merged in. Once this PR and #206 are merged, we'll release a new version of the lib.

Copy link
Collaborator

@timjklein36 timjklein36 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea is sound, I just have a few comments/questions.

Comment on lines 322 to 326
return (
self.model._meta.fields
+ self.model._meta.many_to_many
+ tuple(self.model._meta.private_fields)
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@berinhard Any reason we can't use self.model._meta.get_fields() here and then remove related_objects via set subtraction (as they are retrieved via another method already, get_related)?

Suggested change
return (
self.model._meta.fields
+ self.model._meta.many_to_many
+ tuple(self.model._meta.private_fields)
)
return set(self.model._meta.get_fields()) - set(self.get_related())

The results may be more complex to read, but it would be future-proof going forward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think using Django's public API in _meta over private API is preferable too. The get_related method itself could also be updated to only use public API methods (perhaps also other areas in the Baker class), but that seems outside the scope of this PR at least. I've updated this method according to the suggestion.

objects = baker.make(models.Profile, _quantity=2)
dummies = baker.make(
models.DummyGenericForeignKeyModel,
content_object=iter(objects),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when no value is provided for this field? Consider adding a test for it.

Also, what happens to the content_type and object_id fields? Maybe there should be a test for those as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the test to also verify the content_type and object_id field contain the expected values.

I'm unclear on whether the question "What happens when no value is provided for this field?" means what happens for the case baker.make(models.DummyGenericForeignKeyModel) or the case baker.make(models.DummyGenericForeignKeyModel, content_object=iter([])), since this PR specifically fixes iter being broken on a GFK. Did you want a test for the latter? The former case is already present in an existing test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think you are right: the case I was thinking of is already covered. No action needed.

@anapaulagomes
Copy link
Contributor

Thank you for your contribution @abbottc! 🎉

@anapaulagomes anapaulagomes merged commit 5e2a6e8 into model-bakers:main Jun 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants